/****************************************************************
Program: Lawyers_Regressions.do
Authors: Agan, Freedman, & Owens
Paper: Is Your Lawyer a Lemon (Review of Economics and Statistics)
Updated: July 1, 2019
Description: Program to run full set of regressions.
Data Requirements: Called by Lawyers_Master.do.
Software: Originally run in Stata-MP 14.2.
Other Notes: Calls on reghdfe.ado (version 3.2.9 21feb2016).
****************************************************************/

* Set SE clustering variable
local ses attybarcard

* Run LASSO? (y/n)
local lasso n;

* Clean up
estimates drop _all
cap drop _I*
xi i.race
cap drop mult_off
by sid, sort: egen mult_off=sum(sample)
replace mult_off=0 if mult_off==1
replace mult_off=1 if mult_off>0 & mult_off!=.
cap drop bgnum
egen bgnum=group(bg)
cap drop length_cat
sum case_length, detail
gen length_cat=1 if case_length<=r(p25)
replace length_cat=2 if missing(length_cat) & case_length<=r(p50)
replace length_cat=3 if missing(length_cat) & case_length<=r(p75)
replace length_cat=4 if missing(length_cat)
cap egen atty_crime=group(attybarcard offensecode)
cap drop industry_share
cap drop pred_income

* Create instrument for IV
gen industry_share=0
forvalues n=1(1)13{
	replace industry_share=industry_share+industry_`n'_shr*industry_`n'_coef
}
gen pred_income=constant_coef+industry_share

cap drop yeard

gen yeard=dispositionyear
replace yeard=2010 if dispositionyear==2009 & dispositionmonth>=7
replace yeard=2016 if dispositionyear==2015 & dispositionmonth>=11

xi i.appointed*i.yeard, prefix(Y)

drop Yyeard* Yappointed*

foreach n in instability {
	qui sum `n'
	cap gen std_`n'=(`n'-r(mean))/r(sd)
}
cap gen appt_instable=appointed*std_instability

#delimit ;
keep 	appointed case_length guilty dismissed defadj plead_guilty plead_nolo incarcerated reduced lnsent lnfine
		appt_instable instability male age_offense complaint_hist convict_hist hadbondsman race _I* povrate lndist atty_exp pct_appt racematch caseload_retained_30
		atty_year  offense_num court_docket attybarcard pred_income industry_comp sid
		typeofoffense gjuryyear used guilty offensecat
		attyrace court attyfirm_size white_white black_black hisp_hisp apt_* *_appt experience percent_cases_offense_specific yeard attymale;
order  	case_length guilty dismissed defadj plead_guilty plead_nolo incarcerated reduced lnsent lnfine
		male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30;

#delimit cr	
compress
	
** Unconditional **
	
	local spec1 "if used==1  & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(used) vce(cluster `ses') old"
	local spec5 "male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old"
	local spec8 "male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30   (appointed=pred_income industry_comp) if used==1 & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old"
	local spec13 "appt_instable instability male age_offense complaint_hist convict_hist hadbondsman  _I* povrate lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old"
	local spec16 "male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 offense_num_* court_docket_* c.(male-caseload_retained_30)#c.(male-caseload_retained_30) c.(male-caseload_retained_30)#c.(offense_num_*) old"
	
	foreach outcome in dismissed defadj guilty plead_guilty plead_nolo incarcerated reduced lnsent lnfine case_length {

		qui reghdfe `outcome' appointed `spec1'
		estimates store `outcome'1

		qui reghdfe `outcome' appointed `spec5'
		estimates store `outcome'5

		qui reghdfe `outcome'  `spec8'
		estimates store `outcome'IV

		qui reghdfe `outcome' appointed `spec13' 
		estimates store `outcome'Int
		
		if "`lasso'"=="y" & "$samp"=="Base" & ("`outcome'"=="dismissed" | "`outcome'"=="defadj" | "`outcome'"=="guilty") { 
			noisily di "Running Lasso on Unconditional Sample (Outcome: `outcome')"
			qui tab offense_num, gen(offense_num_)
			qui tab court_docket, gen(court_docket_)
			xtset atty_year
			pdslasso `outcome' appointed (`spec16'), fe cluster(attybarcard)
			estimates store `outcome'Lasso	
			capture drop offense_num_* court_docket_*
		}

	}

	# delimit ;
	foreach outcome in 0 1 {;

		qui reghdfe case_length appointed if used==1  & gjuryyear>2000 & !mi(typeofoffense) & hadbondsman==`outcome' ,  absorb(used) vce(cluster `ses') old;
			estimates store case_length`outcome'1;

		qui reghdfe case_length appointed male age_offense complaint_hist convict_hist  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 
			& gjuryyear>2000 & !mi(typeofoffense) & hadbondsman==`outcome' ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old;
			estimates store case_length`outcome'5;

		qui reghdfe case_length appointed appt_instable male age_offense complaint_hist convict_hist  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 
			& gjuryyear>2000 & !mi(typeofoffense) & hadbondsman==`outcome' ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old;
			estimates store case_length`outcome'Int;

	};

# delimit cr
compress

** Conditional on Guilty **

	local spec1 "if used==1  & guilty==1 & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(used) vce(cluster `ses') old"
	local spec5 "male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & guilty==1 & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old"
	local spec8 "male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 (appointed=pred_income industry_comp) if used==1 & guilty==1 & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year offense_num court_docket) vce(cluster `ses') old"
	local spec13 "appt_instable instability male age_offense complaint_hist convict_hist hadbondsman  _I* povrate lndist atty_exp pct_appt racematch caseload_retained_30 if used==1 & guilty==1 & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old"
	local spec16 "male age_offense complaint_hist convict_hist hadbondsman  _I* povrate instability lndist atty_exp pct_appt racematch caseload_retained_30 offense_num_* court_docket_* c.(male-caseload_retained_30)#c.(male-caseload_retained_30) c.(male-caseload_retained_30)#c.(offense_num_*) old"
	
	foreach outcome in incarcerated lnsent lnfine {

		reghdfe `outcome' appointed `spec1'
		estimates store `outcome'1_g

		reghdfe `outcome' appointed  `spec5'
		estimates store `outcome'5_g

		qui reghdfe `outcome' `spec8'
		estimates store `outcome'IV_g

		qui reghdfe `outcome' appointed `spec13' 
		estimates store `outcome'Int_g

		if "`lasso'"=="y" & "$samp"=="Base" & ("`outcome'"=="incarcerated") { 
			noisily di "Running Lasso on Unconditional Sample (Outcome: `outcome')"
			qui tab offense_num, gen(offense_num_)
			qui tab court_docket, gen(court_docket_)
			xtset atty_year
			pdslasso `outcome' appointed (`spec16') if guilty==1, fe cluster(attybarcard)
			estimates store `outcome'Lasso_g	
			capture drop offense_num_* court_docket_*
		}
	}

** Output Results **

	* Main Effects;
	# delimit ;
	estout dismissed5 defadj5 guilty5 incarcerated5_g  
		using TableA5_$samp.csv, 
		c(b(fmt(4) star) se(fmt(4) par([ ])   )) keep(appointed) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) delim(";") replace;

	if "$samp"=="Base" {;

		* Complete Results;
		# delimit ;
		estout reduced* dismissed* defadj* plead_nolo* plead_guilty* 
			guilty* incarcerated* lnsent* lnfine* case_length*  
			using TablesA1A2.csv, 
			c(b(fmt(4) star) se(fmt(4) par([ ])) ) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) delim(";") replace;
	
		* Unstable Neighborhoods Interactions;
		# delimit ;
		estout dismissedInt defadjInt guiltyInt incarceratedInt_g 
			using TableA5_Int.csv, 
			c(b(fmt(4) star) se(fmt(4) par([ ]))   ) keep(appointed appt_instable) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) delim(";") replace;

		* IV;
		# delimit ;
		estout dismissedIV defadjIV guiltyIV incarceratedIV_g   
			using TableA5_IV.csv, 
			c(b(fmt(4) star) se(fmt(4) par([ ]))  ) keep(appointed) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) delim(";") replace;

		estimates drop reduced* dismissed* defadj* plead_nolo* plead_guilty* 
			guilty* incarcerated* lnsent* lnfine*; 

	};



# delimit cr

** Regressions by Race and with Interactions **

if "$samp"=="Base" {

	cap gen black_appt=appointed if attyrace=="B"
	cap gen white_appt=appointed if attyrace=="W"
	cap gen hisp_appt=appointed if attyrace=="L"

	replace black_appt=0 if missing(black_appt)
	replace white_appt=0 if missing(white_appt)
	replace hisp_appt=0 if missing(hisp_appt)

	local Blist "black_black apt_bb"
	local Wlist "white_white apt_ww"
	local Llist "hisp_hisp apt_hh"

	local B2list "white_appt hisp_appt"
	local W2list "black_appt hisp_appt"
	local L2list "black_appt white_appt"

	keep if attyrace=="W" | attyrace=="B" | attyrace=="L"

	cap egen atty_year=group(attybarcard gjuryyear)
	cap gen allplea=plead_guilty+plead_nolo

	foreach race in W B L {

		foreach outcome in case_length allplea guilty lnsent dismissed defadj plead_guilty plead_nolo incarcerated reduced lnfine {

			qui reghdfe `outcome' appointed ``race'list' male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt caseload_retained_30 _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) & race=="`race'" ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
			estimates store `race'`outcome'1

			qui reghdfe `outcome' appointed ``race'2list' male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt caseload_retained_30 _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) & race=="`race'" ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
			estimates store `race'`outcome'2

		}

		foreach outcome in lnsent lnfine incarcerated {

			qui reghdfe `outcome' appointed ``race'list' male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt caseload_retained_30 _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) & race=="`race'" & guilty==1,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
			estimates store `race'`outcome'_g1

			qui reghdfe `outcome' appointed ``race'2list' male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt caseload_retained_30 _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) & race=="`race'" & guilty==1,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
			estimates store `race'`outcome'_g2

		}

		#delimit ;
		estout  `race'dismissed* `race'defadj* `race'guilty* `race'incarcerated*  `race'case_length* using Table4_`race'.csv, 
			c(b(fmt(4) star) se(fmt(4) par([ ])) ) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) replace;
		estout `race'case_length* `race'allplea* `race'guilty* `race'lnsent* `race'dismissed* `race'defadj* `race'plead_guilty* `race'plead_nolo* `race'incarcerated* `race'reduced* `race'lnfine*  
			using TableA6_`race'.csv, 
			c(b(fmt(4) star) se(fmt(4) par([ ])) ) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) delim(";") replace;
		#delimit cr

	}

	#delimit ;
	estout  Bdismissed1 Bdismissed2 Bdefadj1 Bdefadj2 Bguilty1 Bguilty2 Bincarcerated_g1 Bincarcerated_g2 
			Ldismissed1 Ldismissed2 Ldefadj1 Ldefadj2 Lguilty1 Lguilty2 Lincarcerated_g1 Lincarcerated_g2 
			Wdismissed1 Wdismissed2 Wdefadj1 Wdefadj2 Wguilty1 Wguilty2 Wincarcerated_g1 Wincarcerated_g2
			using Table4.csv, 
			c( b(fmt(4) star  ) 
			se(fmt(4) par([ ])) ) drop(male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt caseload_retained_30 _I* black_black white_white hisp_hisp) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) replace;
	#delimit cr

	keep if attyrace=="W" | attyrace=="B" | attyrace=="L"
	cap gen inter=.
	cap egen atty_year=group(attybarcard gjuryyear)

	cap drop reg_exp
	cap drop pct_spec
	cap drop post_fee
	cap drop post_fee2

	gen reg_exp=experience/10
	gen pct_spec=percent_cases_offense_specific*100 
	replace pct_appt=pct_appt*100
	gen post_fee=yeard>=2010
	gen post_fee2=yeard>=2015

	cap drop judgeselect
	gen judgeselect=(court=="D226" | court=="D186" | court=="D379")

	cap drop solo
	gen solo=trim(attyfirm_size)=="Solo"
	cap drop case30

	gen case30=caseload_retained_30
		
	foreach choice in lndist reg_exp attymale pct_appt judgeselect solo case30 pct_spec {

		replace inter=appointed*`choice'

		foreach outcome in case_length allplea guilty lnsent dismissed defadj plead_guilty plead_nolo incarcerated reduced lnfine {

			qui reghdfe `outcome' appointed inter male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt racematch reg_exp caseload_retained_30 _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
			estimates store `choice'`outcome'1

		}

		foreach outcome in  lnsent  lnfine incarcerated {

			qui reghdfe `outcome' appointed inter male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt racematch reg_exp caseload_retained_30  _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) & guilty==1,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
			estimates store `choice'`outcome'_g1

		}
	}

	cap gen inter1=appointed*post_fee
	cap gen inter2=appointed*post_fee2

	foreach outcome in case_length allplea guilty lnsent dismissed defadj plead_guilty plead_nolo incarcerated reduced lnfine {

		qui reghdfe `outcome' appointed inter1 inter2 post_fee post_fee2 male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt racematch reg_exp caseload_retained_30 _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) ,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
		estimates store post_fee`outcome'1

	}

	foreach outcome in  lnsent  lnfine incarcerated {

		qui reghdfe `outcome' appointed inter1 inter2 post_fee post_fee2 male age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt racematch reg_exp caseload_retained_30  _I* if used==1  & gjuryyear>2000 & !mi(typeofoffense) & guilty==1,  absorb(atty_year  offense_num court_docket) vce(cluster `ses') old
		estimates store post_fee`outcome'_g1

	}

	foreach choice in lndist reg_exp attymale pct_appt judgeselect solo case30 pct_spec post_fee {

		#delimit ;
			estout `choice'dismissed* `choice'defadj* `choice'guilty* `choice'incarcerated_g*  using Table5_`choice'.csv, 
				c(b(fmt(4) star) se(fmt(4) par([ ])) ) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) replace;
		#delimit cr

		# delimit ;
			estout `choice'reduced* `choice'dismissed* `choice'defadj* `choice'plead_nolo* `choice'plead_guilty* 
				`choice'guilty* `choice'incarcerated* `choice'lnsent* `choice'lnfine* `choice'case_length*  
				using TableA6_`choice'.csv, 
				c(b(fmt(4) star) se(fmt(4) par([ ])) ) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) delim(";") replace;
		#delimit cr
	}

	#delimit ;
	estout lndistdismissed1 lndistdefadj1 lndistguilty1 lndistincarcerated_g1 
		reg_expdismissed1 reg_expdefadj1 reg_expguilty1 reg_expincarcerated_g1 
		attymaledismissed1 attymaledefadj1 attymaleguilty1 attymaleincarcerated_g1  
		pct_specdismissed1 pct_specdefadj1 pct_specguilty1 pct_specincarcerated_g1  
		post_feedismissed1 post_feedefadj1 post_feeguilty1 post_feeincarcerated_g1   
		 using Table6.csv, 
		c(b(fmt(4) star  ) se(fmt(4) par([ ]) ) ) keep(appointed inter) starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) replace;


	#delimit ;
	estout case_length5 case_length05  case_length15
		Bcase_length1 Lcase_length1 Wcase_length1
		lndistcase_length1 reg_expcase_length1 attymalecase_length1
		pct_speccase_length1 post_feecase_length1
		 using Table6.csv, 
			c(b(fmt(4) star ) 
			se(fmt(4) par([ ]) ) ) 
			drop(male racematch reg_exp age_offense povrate instability complaint_hist convict_hist hadbondsman lndist atty_exp pct_appt caseload_retained_30 _I* black_black white_white hisp_hisp)
			starlevels(+ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2_a N) replace;
			
	#delimit cr

}

